package com.w3cnet.leetcode.controller;

import org.springframework.util.NumberUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.*;


@RequestMapping(value = "/leetcode/2021")
@RestController
public class LeetCode2021Controller {
    /**
     * 罗马数字转整数
     * @return
     */
    @RequestMapping(value = "/roman_to_integer/{s}")
    public Integer romanToInteger(@PathVariable String s) {
        Map<Character, Integer> romanDic = new HashMap<Character, Integer>() {{
            put('I', 1);
            put('V', 5);
            put('X', 10);
            put('L', 50);
            put('C', 100);
            put('D', 500);
            put('M', 1000);
        }};

        Integer result = 0;
        int n = s.length();
        for (int i = 0; i < n; i++) {
            int value = romanDic.get(s.charAt(i));
            if (i < n - 1 && value < romanDic.get(s.charAt(i + 1))) {
                result -= value;
            } else {
                result += value;
            }
        }

        return result;
    }

    /**
     * 换酒问题
     * @param numBottles
     * @param numExchange
     * @return
     */
    @RequestMapping("/water-bottles")
    public int waterBottles(int numBottles, int numExchange) {
        Integer result = numBottles;

        while (numBottles >= numExchange) {
            result += numBottles / numExchange;
            numBottles = numBottles / numExchange + (numBottles % numExchange);
        }

        return result;
    }

    @GetMapping("/lengthOfLongestSubstring")
    public int lengthOfLongestSubstringApi(HttpServletRequest request) {
        String s = request.getParameter("s");
        if (s == null) s = "";

        return lengthOfLongestSubstring2(s);
    }

    /**
     * 无重复字符的最长子串
     * @param s
     * @return
     * @remark 给定一个字符串，请你找出其中不含有重复字符的 最长子串 的长度。
     */
    public int lengthOfLongestSubstring(String s) {
        Set<Character> chars = new HashSet<>();
        int lens = s.length();
        int longest = 0;

        for (int i=0; i<lens; i++) {
            if (i !=0) {
                chars.remove(s.charAt(i-1));
            }

            for (int j=i; j<lens; j++) {
                if (chars.contains(s.charAt(j))) {
                    i = j - 1;
                    break;
                }
                chars.add(s.charAt(j));
            }

            longest = Math.max(longest, chars.size());
        }

        return longest;
    }

    public int lengthOfLongestSubstring2(String s) {
        Set<Character> chars = new HashSet<Character>();
        int lens = s.length();
        int cursor = -1, longest = 0;

        for (int i = 0; i < lens; i++) {
            if (i != 0) {
                chars.remove(s.charAt(i - 1));
            }

            while (cursor + 1 < lens && !chars.contains(s.charAt(cursor + 1))) {
                chars.add(s.charAt(cursor + 1));
                cursor++;
            }

            longest = Math.max(longest, cursor - i + 1);
        }

        return longest;
    }

    /**
     * 数组异或操作
     * @param n
     * @param start
     * @return
     */
    public int xorOperation(int n, int start) {
        Integer result = 0;

        int nums[] = new int[n];
        for (int i=0; i<n; i++) {
            nums[i] = start + 2 * i;
        }

        for (int i=0; i<nums.length; i++) {
            if (i==0) {
                result = nums[i];
                continue;
            }

            result = result ^ nums[i];
        }

        return result;
    }

    @GetMapping("/xorOperation")
    public int xorOperation(HttpServletRequest request) throws IOException {
        String n = request.getParameter("n");
        if (n == null) n = "";

        int n2 = -1;
        try {
            n2 = Integer.parseInt(n);
        } catch (NumberFormatException e) {
            OutputStreamWriter out = new OutputStreamWriter (System.out,"GB2312");
            out.write("参数n不是整数");
            out.flush();
            return -1;
        }

        String start = request.getParameter("start");
        if (start == null) start = "";
        int start2 = -1;
        try {
            start2 = Integer.parseInt(start);
        } catch (NumberFormatException e) {
            System.out.println("参数start不是整数");
            return -1;
        }

        return xorOperation(n2, start2);
    }

}
